package com.zyh.local;

/**
 * @Author zyh
 * @Date 2024/8/9 11:15
 * @Description ThreadLocal
 */

public class ThreadLocalDemo1 {
    public static void main(String[] args) {
        //创建学生对象
//        Student student = new Student();
        //创建任务对象
        MyTask myTask = new MyTask();
        //开启三个学生线程
        new Thread(myTask, "学生A").start();
        new Thread(myTask, "学生B").start();
        new Thread(myTask, "学生C").start();
    }
}

//任务类
class MyTask implements Runnable {
    //    private Student student;
    public MyTask() {
    }

    /*public MyTask(Student student) {
        this.student = student;
    }*/
    //创建MyThreadLocal对象
    MyThreadLocal myThreadLocal = new MyThreadLocal();

    @Override
    public void run() {
        Student student = myThreadLocal.get();
        student.setName(Thread.currentThread().getName());
        //睡1秒
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(student.getName());
    }
}
